package com.zbxx.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.zbxx.domain.Department;
import com.zbxx.domain.Employee;
import com.zbxx.domain.PageBean;
import com.zbxx.service.DepartmentService;
import com.zbxx.service.EmployeeService;

import javax.annotation.Resource;
import java.util.List;

/*
 * 员工管理的action类
 */
public class EmployeeAction extends ActionSupport implements ModelDriven<Employee> {
    //模型驱动需要使用的对象
    private Employee employee = new Employee();
    //接受当前页数
    private Integer currPage=1;

    //接受参数
    @Override
    public Employee getModel() {
        return employee;
    }

    //员工注入业务层
    @Resource
    private EmployeeService employeeService;
    //注入部门的业务层
    @Resource
    private DepartmentService departmentService;


    //    登录执行的方法
    public String login() {
        System.out.println("aaaaaaaaa");
        //调用业务的类
        Employee existEmployee = employeeService.login(employee);
        if (existEmployee == null) {
            //登录失败
            this.addActionError("用户名或密码错误");
            return INPUT;
        } else {
            //登录成功
            ActionContext.getContext().getSession().put("existEmployee",existEmployee);
            return SUCCESS;
        }
    }

    //分页查询员工的执行方法~
    public String findAll(){
       PageBean<Employee> pageBean= employeeService.findByPage(currPage);
       //存到值栈中
       ActionContext.getContext().getValueStack().push(pageBean);
        return "findAll";
    }
    //跳转到添加员工执行的方法
    public String saveUI(){
        //查询所有部门
         List<Department> list= departmentService.findAll();
        System.out.println("list2222222====="+list);
        ActionContext.getContext().getValueStack().set("list",list);
        return "saveUI";
    }
        //保存员工的执行方法
    public String save(){
        employeeService.save(employee);
        return "saveSuccess";
    }
    //编辑员工执行的方法
    public  String edit(){
        //根据员工的id查询员工
         employee=employeeService.findById(employee.getEid());
        System.out.println("111111==="+employee.getEid());
        //查询所有的部门
        List<Department> list= departmentService.findAll();
        System.out.println("list====="+list);
        ActionContext.getContext().getValueStack().set("list",list);//存到值栈中
        return "editSuccess";
    }
    //修改员工的执行方法
    public String update(){
        employeeService.update(employee);
        return "updateSuccess";
    }
    //删除员工的方法
    public String delete(){
        employee=employeeService.findById(employee.getEid());
        employeeService.delete(employee);
        return "deleteSuccess";
    }

    public Employee getEmployee() {
        return employee;
    }

    public void setEmployee(Employee employee) {
        this.employee = employee;
    }

    public Integer getCurrPage() {
        return currPage;
    }

    public void setCurrPage(Integer currPage) {
        this.currPage = currPage;
    }

    public EmployeeService getEmployeeService() {
        return employeeService;
    }

    public void setEmployeeService(EmployeeService employeeService) {
        this.employeeService = employeeService;
    }

    public DepartmentService getDepartmentService() {
        return departmentService;
    }

    public void setDepartmentService(DepartmentService departmentService) {
        this.departmentService = departmentService;
    }

}
